package com.yjz.study.adapter.detail

import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.widget.ImageView
import android.widget.TextView
import com.yjz.common.extend.loadUrl
import com.yjz.study.R
import com.yjz.study.http.entity.res.SliderImage
import com.yjz.ui.banner.BannerLayout
import com.yjz.ui.banner.core.BannerAdapter
import com.yjz.ui.banner.core.BannerModel
import com.yjz.ui.banner.indicator.NumberIndicator
import com.yjz.ui.recycler.item.HiDataItem
import com.yjz.ui.recycler.item.HiViewHolder

/**
 *作用 : 商品详情头部
 *作者 : yjz
 *时间 : 2025/5/14 15:27
 */
class GoodsDetailHeadItem(
    private val sliderImages: List<SliderImage>?,
    private val price: String?,
    private val completedNumText: String?,
    private val goodsName: String?
) : HiDataItem<Any, HiViewHolder>() {

    override fun onBindData(holder: HiViewHolder, position: Int) {
        val context = holder.itemView.context ?: return

        val bannerLayout = holder.itemView.findViewById<BannerLayout>(R.id.hi_banner)
        val priceTv = holder.itemView.findViewById<TextView>(R.id.price)
        val saleDescTv = holder.itemView.findViewById<TextView>(R.id.sale_desc)
        val titleTv = holder.itemView.findViewById<TextView>(R.id.title)

        val bannerItems = arrayListOf<BannerModel>()
        sliderImages?.forEach {
            val bannerMo = BannerModel(it.url)
            bannerItems.add(bannerMo)
        }
        bannerLayout.setIntervalTime(2000)
        bannerLayout.setBannerIndicator(NumberIndicator(context))
        bannerLayout.setBindAdapter { viewHolder: BannerAdapter.ViewHolder?, mo: BannerModel?, position: Int ->
            val imageView = viewHolder?.rootView as? ImageView
            mo?.let { imageView?.loadUrl(it.url) }
        }
        bannerLayout.setBannerData(bannerItems)

        priceTv.text = spanPrice(price)
        saleDescTv.text = completedNumText
        titleTv.text = goodsName
    }

    private fun spanPrice(price: String?): CharSequence {
        if (TextUtils.isEmpty(price)) return ""
        val ss = SpannableString(price)
        ss.setSpan(AbsoluteSizeSpan(18, true), 1, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ss
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_header_item
    }
}